import { createSlice, PayloadAction } from '@reduxjs/toolkit';

const namespace = 'global';

export interface GlobalState {
  // userInfo?: {
  //   name?: string;
  //   avatar?: string;
  //   job?: string;
  //   organization?: string;
  //   location?: string;
  //   email?: string;
  //   permissions: Record<string, string[]>;
  // };
  // userDate?: Date;
  // userLoading?: boolean;
  themeColor?: 'light' | 'dark';
  theme?: 'system' | 'light' | 'dark';
}

const initialState: GlobalState = {
  // userInfo: {
  //   permissions: {},
  // },
  themeColor: 'light',
  theme: 'system',
};
// 创建带有命名空间的reducer
const globalSlice = createSlice({
  name: namespace,
  initialState,
  reducers: {
    // updateUserInfo: (state, action: PayloadAction<GlobalState>) => {
    //   const { userInfo = initialState.userInfo, userLoading } = action.payload;
    //   state.userInfo = userInfo;
    //   state.userLoading = userLoading;
    // },
    setTheme: (state, action: PayloadAction<GlobalState>) => {
      const { theme } = action.payload;
      if (theme != state.theme) {
        state.theme = theme;
      }
    },
    setThemeColor: (state, action: PayloadAction<GlobalState>) => {
      const { themeColor } = action.payload;
      state.themeColor = themeColor;
    },
    // updateUserReq: (state) => {
    //   state.userDate = new Date();
    // },
  },
  extraReducers: () => {},
});

export const {
  // updateSettings
  // updateUserInfo,
  setThemeColor,
  setTheme,
  // updateUserReq,
} = globalSlice.actions;

export default globalSlice.reducer;
